package com.wwwarehouse.contract.orders.import_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.adapter.ImportOrdersPagersAdapter;
import com.wwwarehouse.contract.bean.GetShopIdBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * 订单导入设置主页
 * Created by ShineF on 2017/8/11 0011.
 */

public class ImportOrdersPagersFragment extends BaseFragment implements ImportOrdersPagersAdapter.OnItemClickListener {

    private View mRootView = null;
    private RecyclerView mRvContent = null;
    private boolean isMeasured = false;
    private ImportOrdersPagersAdapter mAdapter;
    private ArrayList<GetShopIdBean.ListBean> dataList = null;
    private int mType;
    private GetShopIdBean mData;
    private StateLayout mStateLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_import_orders_pagers, container, false);
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRvContent = (RecyclerView) mRootView.findViewById(R.id.rv_content);
        mStateLayout = $(R.id.custom_state_layout);
        mRvContent.addItemDecoration(new SpaceItemDecoration(10, 10));
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3));
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ImportOrdersPagersFragment) {
            mActivity.setTitle(R.string.select_shop);
        }
    }

    @Override
    public void requestDatas() {

    }


    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            Bundle bundle = getArguments();
            if (null != bundle) {
                int mPosition = bundle.getInt("position");
                mType = bundle.getInt("type");
                if (bundle.getParcelableArrayList("data") != null && mPosition == 0) {
                    dataList = bundle.getParcelableArrayList("data");
                    ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        public boolean onPreDraw() {
                            if (!isMeasured) {
                                setHeight(mRvContent.getMeasuredHeight());
                            }
                            return true;
                        }
                    });
                    if (isMeasured && mAdapter != null) {
                        mRvContent.setAdapter(mAdapter);
                    }
                } else {
                    if (dataList == null) {
                        mStateLayout.showProgressView(true);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("buId", "10001878");
//                        map.put("buName", "tester");
//                        map.put("buName", "");
                        map.put("buName", "店铺");
                        map.put("sort", "CREATE_TIME DESC");
//                        map.put("sort", "CREATE_TIME");
                        map.put("page", String.valueOf(mPosition + 1));
                        map.put("size", "9");
                        loadData(ContractConstant.GET_IMPORT_ORDERS_SHOP, map, 1);
                    }
                }
            }
        }
        return null;
    }

    public void setHeight(int height) {
        isMeasured = true;
        if (mAdapter == null && isAdded()) {
            mAdapter = new ImportOrdersPagersAdapter(mActivity, dataList, height, mType);
            mRvContent.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    }

    @Override
    public void ItemClickListener(int position, View view, GetShopIdBean.ListBean data) {
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        ImportOrdersDetailsFragment fragment = new ImportOrdersDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", mType);
        bundle.putInt("status", data.getStatus());
        bundle.putParcelable("data", data);
        fragment.setArguments(bundle);
        pushFragment(fragment);
    }

    public void loadData(String url, Map map, final int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if (what == 1) {
                    if ("0".equals(commonClass.getCode())) {
                        mData = JSON.parseObject(commonClass.getData().toString(), GetShopIdBean.class);
                        setData();
                    }
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {
                mStateLayout.showContentView();
            }
        }, what);
    }

    public void setData() {
        dataList = mData.getList();
        ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!isMeasured) {
                    setHeight(mRvContent.getMeasuredHeight());
                }
                return true;
            }
        });
    }
}
